package com.edu.study.base.rest;

import com.edu.base.common.utils.MD5Util;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLEncoder;
import java.util.Date;

/**
 * @author zuowei
 * @version 1.0
 * @date 2021-06-29 21:41
 */
//@Api(tags = "百度翻译")
@RestController
@RequestMapping("/api/translate")
public class AppWordtranslateController {

    String AppId = "20210629000875560";
    String key = "ue7HHzupxAVeDwncoHfQ";
    String url = "http://fanyi-api.baidu.com/api/trans/vip/translate";
    String salt = String.valueOf(System.currentTimeMillis());//DateUtils.parseDateToStr("yyyyMMddHHmmss",new Date());

    //@ApiOperation("英文单词翻译成中文")
    @ApiImplicitParam(name = "word", value = "英文单词", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getWord/{word}")
    public String TranslateWord(@PathVariable String word){
        StringBuilder param = new StringBuilder();
        param.append("q=" + encode(word));
        param.append("&from="+encode("en"));
        param.append("&to="+ encode("zh"));
        param.append("&appid=" + encode(AppId));
        param.append("&salt=" + encode(salt));
        param.append("&sign=" + encode(getSign(word)));
        String result = HttpUtils.sendGet(url,param.toString());
        String unicode_Value = result.substring(result.lastIndexOf(":")+2,result.lastIndexOf('"'));
        String value =decodeUnicode(unicode_Value);
        return result.replace(unicode_Value,value);
    }

    /**
     * 对输入的字符串进行URL编码, 即转换为%20这种形式
     *
     * @param input 原文
     * @return URL编码. 如果编码失败, 则返回原文
     */
    public static String encode(String input) {
        if (input == null) {
            return "";
        }

        try {
            return URLEncoder.encode(input, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return input;
    }


    private String getSign(String word){
        String inStr = AppId + word + salt + key;
        String md5Str = MD5Util.string2MD5(inStr);
        System.out.println("md5Str==" + md5Str);
        return md5Str;
    }


    public static String decodeUnicode(final String dataStr) {
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while (start > -1) {
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if (end == -1) {
                charStr = dataStr.substring(start + 2, dataStr.length());
            } else {
                charStr = dataStr.substring(start + 2, end);
            }
            char letter = (char) Integer.parseInt(charStr, 16); // 16进制parse整形字符串。
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }

}
